﻿package bdz.projects.slab.sections {
	import as3.common.ui.menu.core.MenuComponent;
	import as3.common.ui.menu.events.MenuComponentEvent;
	import as3.common.ui.menu.TweenedGridList;
	import as3.common.ui.menu.core.MenuItem;
	import bdz.dms.data.DMSCollection;
	import bdz.dms.vo.SectionVO;
	import bdz.projects.slab.core.SlabSection;
	import bdz.projects.slab.data.SlabSiteData;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import bdz.common.utils.TweenLite;
	import flash.text.*;
	import flash.utils.*
	import bdz.common.utils.BitmapUtils;
	import bdz.projects.slab.events.SlabSiteEvent;
	import as3.common.ui.menu.AnimatedItem;
	import as3.common.ui.menu.events.MenuComponentEvent;
	
	/**
	* ...
	* @author Default
	*/
	public class SlabArtistList extends SlabSection {
		private var gallHeader:GallAppearance;
		private var list:TweenedGridList;
		private var listCover:MovieClip;
		private var arrLeft:SimpleButton;
		private var arrRight:SimpleButton;
		private var siteData:SlabSiteData = SlabSiteData.getInstance();
		private var selectedArtist:String;
		private var subMenu:TweenedGridList;
		function SlabArtistList() {
			
		}
		override protected function initializeComponent(evt:Event):void {
			listCover = new MovieClip();
			listCover.x = 14;
			listCover.y = 110;
			BitmapUtils.createBounds(listCover, 620, 150, 0);
			addChild(listCover);
			
			super.initializeComponent(evt);
			createList();
		}
		private function createSubList(dsource:Array, def:String):void {
			gallHeader = new GallAppearance();
			gallHeader.description = def + " has appeared on the following: ";
			gallHeader.x = listCover.x;
			gallHeader.y = listCover.y + listCover.height + 10;
			addChild(gallHeader);
			
			var tmp = new AnimatedItem(new Object(), new TextFormat());
			tmp = null;
			
			var menuStyle:Object = {embedFonts:true, autoSize:"left", border:false, background:false, backgroundColor:0x222222, antiAliasType:AntiAliasType.NORMAL, gridFitType:GridFitType.PIXEL};
			var menuFontFormat:TextFormat = new TextFormat();
			var fontObj:Object = getDefinitionByName("pixelate");
			menuFontFormat.color = 0x999999;
			
			menuFontFormat.font = new fontObj().fontName;
			menuFontFormat.size = 10;
			subMenu = new TweenedGridList("as3.common.ui.menu.AnimatedItem");
			subMenu.slow_generation = true;
			subMenu.y = Math.ceil(gallHeader.y + gallHeader.height);
			subMenu.orientation = "vertical";
			subMenu.labelField = "section_name";
			subMenu.decoration = "subButtonOver";
			subMenu.outdecoration = "subButtonOut";
			subMenu.menuTextFieldStyle = menuStyle;
			subMenu.menuFontStyle = menuFontFormat;
			subMenu.rollOverColor = 0x000000;
			subMenu.upperCaseLabel = true;
			subMenu.viewableColumns = 3;
			subMenu.xpad = 30;
			subMenu.ypad = -3;
			subMenu.x = Math.ceil(gallHeader.x+12);
			subMenu.maxrows = 8;
			subMenu.scrollDuration = .8;
			subMenu.delay = .01;
			subMenu.edgeDelay = .03;
			subMenu.selectFirstItem = false;
			subMenu.addEventListener(MenuComponentEvent.CLICK, subMenuClicked);
			
			subMenu.arrayItems = dsource;
			addChild(subMenu);
		}
		private function subMenuClicked(evt:MenuComponentEvent):void {
			dispatchEvent(new SlabSiteEvent(SlabSiteEvent.GALLERY_FROM_ARTIST, evt.data));
		}
		private function createList():void {
			var menuStyle:Object = {embedFonts:true, autoSize:"left", border:false, background:false, backgroundColor:0x222222, antiAliasType:AntiAliasType.NORMAL, gridFitType:GridFitType.PIXEL};
			var menuFontFormat:TextFormat = new TextFormat();
			var fontObj:Object = getDefinitionByName("xpdr");
			menuFontFormat.color = 0x666666;
			menuFontFormat.font = new fontObj().fontName;
			menuFontFormat.size = 8;
			
			list = new TweenedGridList("as3.common.ui.menu.core.MenuItem");
			list.addEventListener("listReady", listReady);
			list.labelField = "section_name";
			list.y = listCover.y;
			list.x = listCover.x;
			list.menuTextFieldStyle = menuStyle;
			list.menuFontStyle = menuFontFormat;
			list.rollOverColor = 0xffffff;
			list.upperCaseLabel = false;
			list.decoration = "artButtonOver";
			list.outdecoration = "artButtonOut";
			list.xpad = 30;
			list.viewableColumns = 3;
			list.maxrows = 8;
			list.scrollDuration = .8;
			list.delay = .01;
			list.edgeDelay = .03;
			list.selectFirstItem = false;
			list.addEventListener(MenuComponentEvent.CLICK, checkArtistDetail);
			list.arrayItems = siteData.getArtistList();
			addChild(list);
			
		}
		private function listReady(evt:Event):void {
			setUpArrows();
		}
		private function onGalleryLoaded(evt:SlabSiteEvent):void {
			checkArtistsExhibit();
		}
		private function checkArtistsExhibit():void {
			removeFeedback();
			var pgall:DMSCollection = siteData.getPublicGalleries();
			var coll:Array = new Array();
			for each(var svo:SectionVO in pgall) {
				if (svo.getProperty("artist_list").indexOf(selectedArtist) != -1) {
					coll.push(svo);
				}
			}
			if (gallHeader && contains(gallHeader)) {
				try{
					removeChild(gallHeader);
				}catch (e:Error) {
					
				}
				try{
					removeChild(subMenu);
				}catch (e:Error) {
					
				}
			}
			if (coll.length > 1) {
				createSubList(coll, selectedArtist);
			}else {
				if(coll.length == 0){
					gallHeader = new GallAppearance();
					gallHeader.description = selectedArtist + " has not yet appeared on any SLab exhibits. ";
					gallHeader.x = listCover.x;
					gallHeader.y = listCover.y + listCover.height + 10;
					addChild(gallHeader);
				}else {
					dispatchEvent(new SlabSiteEvent(SlabSiteEvent.GALLERY_FROM_ARTIST, coll[0]));
				}
			}
		}
		private function checkArtistDetail(evt:MenuComponentEvent):void {
			selectedArtist = evt.data.section_name;
			siteData.addEventListener(SlabSiteEvent.PUBLIC_GALLERY_LOADED, onGalleryLoaded);
			if (siteData.getPublicGalleries()) {
				checkArtistsExhibit();
			}else {
				showFeedback();
			}
		}
		private function removeFeedback():void {
			
		}
		private function showFeedback():void {
			
		}
		private function scrollRight(evt:MouseEvent):void{
			
			list.scrollLeft();
			if(list.curhscroll == list.maxhscroll){
				arrRight.visible = false;
			}
			if(list.curhscroll){
				arrLeft.visible = true;
			}
		}
		
		private function scrollLeft(evt:MouseEvent):void{
			
			list.scrollRight();
			if(list.curhscroll < list.maxhscroll){
				arrRight.visible = true;
			}
			if(!list.curhscroll){
				arrLeft.visible = false;
			}
		}
		
		private function setUpArrows():void {
			if(list.maxhscroll){
				arrLeft = new arrowLeft();
				arrRight = new arrowRight();
				arrLeft.x = listCover.x-20;
				arrRight.x = listCover.x+listCover.width+20;
				arrLeft.y = arrRight.y = (listCover.y+(listCover.height/2));
				arrRight.alpha = arrLeft.alpha = 0;
				arrLeft.visible = false;
				TweenLite.to(arrRight, .8, {alpha:1});
				TweenLite.to(arrLeft, .8, {alpha:1});
				addChild(arrLeft);
				addChild(arrRight);
				arrRight.addEventListener(MouseEvent.CLICK, scrollRight);
				arrLeft.addEventListener(MouseEvent.CLICK, scrollLeft);
			}
		}
	}
	
}